package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.L1ItemPowerUpdate;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ItemPowerUpdateTable {
  private static final Log _log = LogFactory.getLog(ItemPowerUpdateTable.class);
  
  private static Map<Integer, L1ItemPowerUpdate> _updateMap = new HashMap<>();
  
  private static final ArrayList<Integer> _updateitemidList = new ArrayList<>();
  
  private static ItemPowerUpdateTable _instance;
  
  public static ItemPowerUpdateTable get() {
    if (_instance == null)
      _instance = new ItemPowerUpdateTable(); 
    return _instance;
  }
  
  public static void reload() {
    _instance = new ItemPowerUpdateTable();
    _updateMap.clear();
    _instance.load();
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `server_item_power_update`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int itemid = rs.getInt("itemid");
        if (ItemTable.get().getTemplate(itemid) == null) {
          _log.error("特殊物品升级资料错误: 没有这个编号的道具:" + itemid);
          delete(itemid);
          continue;
        } 
        int nedid = rs.getInt("nedid");
        int type_id = rs.getInt("type_id");
        int order_id = rs.getInt("order_id");
        int mode = rs.getInt("mode");
        int random = rs.getInt("random");
        String allmsg = rs.getString("世界广播");
        L1ItemPowerUpdate value = _updateMap.get(Integer.valueOf(itemid));
        value = new L1ItemPowerUpdate();
        value.set_itemid(itemid);
        value.set_nedid(nedid);
        value.set_type_id(type_id);
        value.set_order_id(order_id);
        value.set_mode(mode);
        value.set_random(random);
        value.setallmsg(allmsg);
        _updateMap.put(Integer.valueOf(itemid), value);
        _updateitemidList.add(Integer.valueOf(itemid));
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log
      .info("载入物品升级资料数量: " + _updateMap.size() + "(" + timer.get() + "ms)");
  }
  
  public int get_original_type(int itemid) {
    int type_id = 0;
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `server_item_power_update` WHERE `itemid` =?");
      pstm.setInt(1, itemid);
      rs = pstm.executeQuery();
      while (rs.next())
        type_id = rs.getInt("type_id"); 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    return type_id;
  }
  
  public int get_original_itemid(int typeid) {
    int itemid = 0;
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement(
          "SELECT * FROM `server_item_power_update` WHERE `type_id` =? AND `order_id` = '0'");
      pstm.setInt(1, typeid);
      rs = pstm.executeQuery();
      while (rs.next())
        itemid = rs.getInt("itemid"); 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    return itemid;
  }
  
  public ArrayList<Integer> get_updeatitemidlist() {
    return _updateitemidList;
  }
  
  public static void delete(int itemid) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `server_item_power_update` WHERE `itemid`=?");
      ps.setInt(1, itemid);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public Map<Integer, L1ItemPowerUpdate> get_type_updatemap(String key) {
    Map<Integer, L1ItemPowerUpdate> updateMap = new HashMap<>();
    L1ItemPowerUpdate tmp = _updateMap.get(key);
    if (tmp != null) {
      int type_id = tmp.get_type_id();
      Iterator<L1ItemPowerUpdate> iterator = _updateMap.values().iterator();
      while (iterator.hasNext()) {
        L1ItemPowerUpdate value = iterator.next();
        if (value.get_type_id() == type_id)
          updateMap.put(Integer.valueOf(value.get_order_id()), value); 
      } 
    } 
    return updateMap;
  }
  
  public L1ItemPowerUpdate get(int key) {
    return _updateMap.get(Integer.valueOf(key));
  }
  
  public L1ItemPowerUpdate gettemplate(String key) {
    return _updateMap.get(key);
  }
  
  public Map<Integer, L1ItemPowerUpdate> map() {
    return _updateMap;
  }
  
  public Map<Integer, L1ItemPowerUpdate> get_type_id(int itemid) {
    Map<Integer, L1ItemPowerUpdate> updateMap = new HashMap<>();
    L1ItemPowerUpdate tmp = _updateMap.get(Integer.valueOf(itemid));
    if (tmp != null) {
      int type_id = tmp.get_type_id();
      Iterator<L1ItemPowerUpdate> iterator = _updateMap.values().iterator();
      while (iterator.hasNext()) {
        L1ItemPowerUpdate value = iterator.next();
        if (value.get_type_id() == type_id)
          updateMap.put(Integer.valueOf(value.get_order_id()), value); 
      } 
    } 
    return updateMap;
  }
}
